﻿Imports System
Imports System.ComponentModel
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.ComponentModel
    Public NotInheritable Class RemovedActivityAction
        Inherits ActivityChangeAction
        ' Methods
        Public Sub New()
            Me.removedActivityIndex = -1
        End Sub

        Public Sub New(ByVal removedActivityIndex As Integer, ByVal originalActivity As Activity, ByVal clonedParentActivity As CompositeActivity)
            MyBase.New(clonedParentActivity)
            Me.removedActivityIndex = -1
            If (originalActivity Is Nothing) Then
                Throw New ArgumentNullException("originalActivity")
            End If
            If (clonedParentActivity Is Nothing) Then
                Throw New ArgumentNullException("clonedParentActivity")
            End If
            Me.originalRemovedActivity = originalActivity
            Me.removedActivityIndex = removedActivityIndex
        End Sub

        Protected Friend Overrides Function ApplyTo(ByVal rootActivity As Activity) As Boolean
            If (rootActivity Is Nothing) Then
                Throw New ArgumentNullException("rootActivity")
            End If
            If Not TypeOf rootActivity Is CompositeActivity Then
                Throw New ArgumentException(SR.GetString("Error_RootActivityTypeInvalid"), "rootActivity")
            End If
            Dim activity As CompositeActivity = [TryCast](rootActivity.TraverseDottedPathFromRoot(MyBase.OwnerActivityDottedPath),CompositeActivity)
            If (activity Is Nothing) Then
                Return False
            End If
            If (Me.removedActivityIndex >= activity.Activities.Count) Then
                Return False
            End If
            activity.DynamicUpdateMode = True
            Try 
                Me.originalRemovedActivity = activity.Activities.Item(Me.removedActivityIndex)
                activity.Activities.RemoveAt(Me.removedActivityIndex)
            Finally
                activity.DynamicUpdateMode = False
            End Try
            Return True
        End Function

        Protected Friend Overrides Function ValidateChanges(ByVal contextActivity As Activity) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.ValidateChanges(contextActivity)
            Dim activity As Activity = contextActivity.TraverseDottedPathFromRoot(Me.originalRemovedActivity.DottedPath)
            If (WorkflowChanges.IsActivityExecutable(activity) AndAlso (activity.ExecutionStatus = ActivityExecutionStatus.Executing)) Then
                errors.Add(New ValidationError(SR.GetString("Error_RemoveExecutingActivity", New Object() { Me.originalRemovedActivity.QualifiedName }), &H11D))
            End If
            Return errors
        End Function


        ' Properties
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        Property OriginalRemovedActivity As Activity
            Public Get
                Return Me.originalRemovedActivity
            End Get
            Friend Set(ByVal value As Activity)
                Me.originalRemovedActivity = value
            End Set
        End Property

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Property RemovedActivityIndex As Integer
            Public Get
                Return Me.removedActivityIndex
            End Get
            Friend Set(ByVal value As Integer)
                Me.removedActivityIndex = value
            End Set
        End Property


        ' Fields
        Private originalRemovedActivity As Activity
        Private removedActivityIndex As Integer
    End Class
End Namespace

